package cn.iocoder.yudao.module.member.service.jobintention;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.jobintention.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.jobintention.JobIntentionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.jobintention.JobIntentionMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 用户求职意向 Service 实现类
 *
 * @author Alan
 */
@Service
@Validated
public class JobIntentionServiceImpl implements JobIntentionService {

    @Resource
    private JobIntentionMapper jobIntentionMapper;

    @Override
    public Long createJobIntention(JobIntentionSaveReqVO createReqVO) {
        // 插入
        JobIntentionDO jobIntention = BeanUtils.toBean(createReqVO, JobIntentionDO.class);
        jobIntention.setUserId(getLoginUserId());
        jobIntentionMapper.insert(jobIntention);
        // 返回
        return jobIntention.getId();
    }

    @Override
    public void updateJobIntention(JobIntentionSaveReqVO updateReqVO) {
        // 校验存在
        validateJobIntentionExists(updateReqVO.getId());
        // 更新
        JobIntentionDO updateObj = BeanUtils.toBean(updateReqVO, JobIntentionDO.class);
        jobIntentionMapper.updateById(updateObj);
    }

    @Override
    public void deleteJobIntention(Long id) {
        // 校验存在
        validateJobIntentionExists(id);
        // 删除
        jobIntentionMapper.deleteById(id);
    }

    private void validateJobIntentionExists(Long id) {
        if (jobIntentionMapper.selectById(id) == null) {
            throw exception(JOB_INTENTION_NOT_EXISTS);
        }
    }

    @Override
    public JobIntentionDO getJobIntention(Long id) {
        return jobIntentionMapper.selectById(id);
    }

    @Override
    public JobIntentionDO getUserJobIntention(Long loginUserId) {
        return jobIntentionMapper.selectOne(new LambdaQueryWrapper<JobIntentionDO>().eq(JobIntentionDO::getUserId,loginUserId));
    }

    @Override
    public PageResult<JobIntentionDO> getJobIntentionPage(JobIntentionPageReqVO pageReqVO) {
        return jobIntentionMapper.selectPage(pageReqVO);
    }

}